#include <stdio.h>
#include "sqlite_api.h"

#include "log_print.h"

#ifdef __cplusplus
extern "C"
{
#endif

    int api_open_sqlite(sqlite3 **db, const char *db_filename)
    {
        int iret = 0;

        if (NULL == db_filename)
        {
            // SQLITE_PRINTF("input param is invaild.\n");
            LOG_PRINTF_ERROR( "input param is invaild.");
            return -1;
        }

        if (NULL != *db)
        {
            // SQLITE_PRINTF("%s db has opened\n", db_filename);
            LOG_PRINTF_ERROR( "%s db has opened", db_filename);
            return -1;
        }

        iret = sqlite3_open(db_filename, db);

        if (SQLITE_OK != iret)
        {
            // SQLITE_PRINTF("open %s db fail, iret is %d.\n", db_filename, iret);
            LOG_PRINTF_ERROR( "open %s db fail, iret is %d.", db_filename, iret);
            return -1;
        }

        SQLITE_PRINTF("Open %s db success.\n", db_filename);
        return 0;
    }

    int api_close_sqlite(sqlite3 *db)
    {
        int iret = 0;

        if (NULL == db)
        {
            // SQLITE_PRINTF("input param invalid.\n");
            LOG_PRINTF_ERROR( "input param invalid.");
            return -1;
        }

        iret = sqlite3_close_v2(db);

        if (SQLITE_OK != iret)
        {
            // SQLITE_PRINTF("close db fail.\n");
            LOG_PRINTF_ERROR( "close db fail.");
            return -1;
        }

        return 0;
    }

    int api_begin_sqlite(sqlite3 *db)
    {
        int iret = 0;
        char *error_message = 0;
        char *sql = "begin;";

        if (NULL == db)
        {
            // SQLITE_PRINTF("input param invalid.\n");
            LOG_PRINTF_ERROR( "input param invalid.");
            return -1;
        }

        iret = sqlite3_exec(db, sql, NULL, NULL, &error_message);

        if (iret != SQLITE_OK)
        {
            // SQLITE_PRINTF("begin %s SQL error: %s\n", sql, error_message);
            LOG_PRINTF_ERROR( "begin %s SQL error: %s", sql, error_message);
            sqlite3_free(error_message);
            return -1;
        }

        SQLITE_PRINTF("begin sql successfully\n");
        return 0;
    }

    int api_commit_sqlite(sqlite3 *db)
    {
        int iret = 0;
        char *error_message = 0;
        char *sql = "commit;";

        if (NULL == db)
        {
            // SQLITE_PRINTF("input param invalid.\n");
            LOG_PRINTF_ERROR( "input param invalid.");
            return -1;
        }

        iret = sqlite3_exec(db, sql, NULL, NULL, &error_message);

        if (iret != SQLITE_OK)
        {
            // SQLITE_PRINTF("commit %s SQL error: %s\n", sql, error_message);
            LOG_PRINTF_ERROR( "commit %s SQL error: %s", sql, error_message);
            sqlite3_free(error_message);
            return -1;
        }

        SQLITE_PRINTF("commit sql successfully\n");
        return 0;
    }

    int api_vacuum_sqlite(sqlite3 *db)
    {
        int iret = 0;
        char *error_message = 0;
        char *sql = "VACUUM;";

        if (NULL == db)
        {
            // SQLITE_PRINTF("input param invalid.\n");
            LOG_PRINTF_ERROR( "input param invalid.");
            return -1;
        }

        iret = sqlite3_exec(db, sql, NULL, NULL, &error_message);

        if (iret != SQLITE_OK)
        {
            // SQLITE_PRINTF("VACUUM %s SQL error: %s\n", sql, error_message);
            LOG_PRINTF_ERROR( "VACUUM %s SQL error: %s", sql, error_message);
            sqlite3_free(error_message);
            return -1;
        }

        SQLITE_PRINTF("VACUUM sql successfully\n");
        return 0;
    }

    int api_create_sqlite_table(sqlite3 *db, const char *sql)
    {
        int iret = 0;
        char *error_message = 0;

        if (NULL == db || NULL == sql)
        {
            // SQLITE_PRINTF("input param invalid.\n");
            LOG_PRINTF_ERROR( "input param invalid.");
            return -1;
        }

        if (SQLITE_BUSY == sqlite3_busy_timeout(db, DBBUSY_TIMEOUT))
        {
            // SQLITE_PRINTF("sqlite3 is busy.");
            LOG_PRINTF_ERROR( "sqlite3 is busy.");
            return -1;
        }

        iret = sqlite3_exec(db, sql, NULL, NULL, &error_message);

        if (iret != SQLITE_OK)
        {
            // SQLITE_PRINTF("Create tables %s SQL error: %s\n", sql, error_message);
            LOG_PRINTF_ERROR( "Create tables %s SQL error: %s", sql, error_message);
            sqlite3_free(error_message);
            return -1;
        }

        SQLITE_PRINTF("Table Created successfully\n");
        return 0;
    }

    int api_insert_sqlite(sqlite3 *db, const char *sql)
    {
        int iret = 0;
        char *error_message = 0;

        if (NULL == db || NULL == sql)
        {
            // SQLITE_PRINTF("input param invalid.\n");
            LOG_PRINTF_ERROR( "input param invalid.");
            return -1;
        }

        if (SQLITE_BUSY == sqlite3_busy_timeout(db, DBBUSY_TIMEOUT))
        {
            // SQLITE_PRINTF("sqlite3 is busy.");
            LOG_PRINTF_ERROR( "sqlite3 is busy.");
            return -1;
        }

        iret = sqlite3_exec(db, sql, NULL, NULL, &error_message);

        if (iret != SQLITE_OK)
        {
            // SQLITE_PRINTF("Insert %s SQL error: %s\n", sql, error_message);
            LOG_PRINTF_ERROR( "Insert %s SQL error: %s", sql, error_message);
            sqlite3_free(error_message);
            return -1;
        }

        SQLITE_PRINTF("Insert sql successfully\n");
        return 0;
    }

    int api_select_sqlite(sqlite3 *db, const char *sql, sqlite3_callback cb_fun, void *data)
    {
        int iret = 0;
        char *error_message = 0;

        if (NULL == db || NULL == sql || NULL == data)
        {
            // SQLITE_PRINTF("input param invalid.\n");
            LOG_PRINTF_ERROR( "input param invalid.");
            return -1;
        }

        if (SQLITE_BUSY == sqlite3_busy_timeout(db, DBBUSY_TIMEOUT))
        {
            // SQLITE_PRINTF("sqlite3 is busy.");
            LOG_PRINTF_ERROR( "sqlite3 is busy.");
            return -1;
        }

        iret = sqlite3_exec(db, sql, cb_fun, data, &error_message);

        if (iret != SQLITE_OK)
        {
            // SQLITE_PRINTF("Select %s SQL error: %s\n", sql, error_message);
            LOG_PRINTF_ERROR( "Select %s SQL error: %s", sql, error_message);
            sqlite3_free(error_message);
            return -1;
        }

        SQLITE_PRINTF("Select sql successfully\n");
        return 0;
    }

    int api_delete_sqlite(sqlite3 *db, const char *sql)
    {
        int iret = 0;
        char *error_message = 0;

        if (NULL == db || NULL == sql)
        {
            // SQLITE_PRINTF("input param invalid.\n");
            LOG_PRINTF_ERROR( "input param invalid.");
            return -1;
        }

        if (SQLITE_BUSY == sqlite3_busy_timeout(db, DBBUSY_TIMEOUT))
        {
            // SQLITE_PRINTF("sqlite3 is busy.");
            LOG_PRINTF_ERROR( "sqlite3 is busy.");
            return -1;
        }

        iret = sqlite3_exec(db, sql, NULL, NULL, &error_message);

        if (iret != SQLITE_OK)
        {
            // SQLITE_PRINTF("Delete %s SQL error: %s\n", sql, error_message);
            LOG_PRINTF_ERROR( "Delete %s SQL error: %s", sql, error_message);
            sqlite3_free(error_message);
            return -1;
        }

        SQLITE_PRINTF("Delete sql successfully\n");
        return 0;
    }

    int api_update_sqlite(sqlite3 *db, const char *sql)
    {
        int iret = 0;
        char *error_message = 0;

        if (NULL == db || NULL == sql)
        {
            // SQLITE_PRINTF("input param invalid.\n");
            LOG_PRINTF_ERROR( "input param invalid.");
            return -1;
        }

        if (SQLITE_BUSY == sqlite3_busy_timeout(db, DBBUSY_TIMEOUT))
        {
            // SQLITE_PRINTF("sqlite3 is busy.");
            LOG_PRINTF_ERROR( "sqlite3 is busy.");
            return -1;
        }

        iret = sqlite3_exec(db, sql, NULL, NULL, &error_message);

        if (iret != SQLITE_OK)
        {
            // SQLITE_PRINTF("Update %s SQL error: %s\n", sql, error_message);
            LOG_PRINTF_ERROR( "Update %s SQL error: %s", sql, error_message);
            sqlite3_free(error_message);
            return -1;
        }

        SQLITE_PRINTF("Update sql successfully\n");
        return 0;
    }

    int api_set_temp_store_directory(sqlite3 *db)
    {
        int iret = 0;
        char *error_message = 0;
        char *sql = "PRAGMA temp_store_directory = '/userdata/dgri/app/DataCenter/bin';";

        if (NULL == db)
        {
            // SQLITE_PRINTF("input param invalid.\n");
            LOG_PRINTF_ERROR("input param invalid.");
            return -1;
        }

        iret = sqlite3_exec(db, sql, NULL, NULL, &error_message);

        if (iret != SQLITE_OK)
        {
            // SQLITE_PRINTF("%s SQL error: %s\n", sql, error_message);
            LOG_PRINTF_ERROR("%s SQL error: %s", sql, error_message);
            sqlite3_free(error_message);
            return -1;
        }

        SQLITE_PRINTF("PRAGMA temp_store_directory = '/userdata/dgri/app/DataCenter/bin'; sql successfully\n");
        return 0;
    }

#ifdef __cplusplus
}
#endif
